<?php 
/**
 * @see Epic_Registry_Abstract
 */
require_once 'Epic/Registry/Abstract.php';

/**
 * Registry class to pass global variables between classes.
 * 
 * @category   Epic
 * @package    Epic_Registry
 * @author     Epic Core Team <rukanhitokiri@gmail.com>
 */
class Epic_Registry_App extends Epic_Registry_Abstract
{
   	/**
  	 * Object registry provides storage for shared objects
  	 * @var array 
  	 */
	private static $_registry = array();

	private static $_instance = null;
	
	public static function getInstance()
	{
		if (null === self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
   	/**
     * Tests if given $key exists in registry
     *
     * @param string $key
     * @return bool
     */
   	public static function has($key)
   	{
   		return isset(self::$_registry[$key]) ?  true : false;
   	}
   
	/**
     * Adds a new variable to the Registry.
     *
     * @param string $key Name of the variable
     * @param mixed $value Value of the variable
     * @throws Epic_Registry_Exception
     */
	public static function set($key, $value = null) {
    	if (!self::has($key))
       		self::$_registry[$key] = $value;
      	else
        	throw new Epic_Registry_Exception('Unable to set variable `' . $key . '`. It was alreadys set.');
	}
    
    /**
     * Returns the value of the specified $key in the Registry.
     *
     * @param string $key Name of the variable
     * @return mixed Value of the specified $key
     */
	public static function get($key)
    {
    	return self::has($key) ? self::$_registry[$key] : null;
   	}
 
    /**
     * Returns the whole Registry as an array.
     *
     * @return array Whole Registry
     */
    public static function getAll()
    {
    	return self::$_registry;
    }
    
    /**
     * Removes a variable from the Registry.
     *
     * @param string $key Name of the variable
     * @throws Epic_Registry_Exception
     * @return bool
     */
    public static function remove($key)
    {
        if (self::has($key)) {
        	unset(self::$_registry[$key]);
            return true;
        }
    	throw new Epic_Registry_Exception('Unable to remove variable `' . $key . '`. Key does not exist .');
    }
  	
	/**
     * Removes all variables from the Registry.
     */
     public static function removeAll()
     {
       	self::$_registry = array();
     } 
} 
